package com.itheima.contorller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domian.Setmeal;
import com.itheima.domian.vo.DishVo;
import com.itheima.domian.vo.QueryVo2;
import com.itheima.domian.vo.SetmealVo;
import com.itheima.mapper.admin.SetmealMapper;
import com.itheima.service.admin.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 12441
 * @Date 2022/8/11 15:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealVo setmealVo){
        log.info("========="+setmealVo.toString());
        setmealService.addSetmeal(setmealVo);

        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<IPage> page(HttpServletRequest httpServerRequest){


        String name = httpServerRequest.getParameter("name");
        Integer page1 = Integer.valueOf(httpServerRequest.getParameter("page"));
        Integer size = Integer.valueOf(httpServerRequest.getParameter("pageSize"));

        Page<QueryVo2> page = new Page<>(page1, size);

        //IPage<QueryVo> queryVoPage = dishService.findByPage(page, new QueryWrapper<>());
        IPage<QueryVo2> queryVoPage = setmealService.findByPage(page, new QueryWrapper<>());

        log.info("========================="+page.getRecords());

        return R.success(queryVoPage,"啊啊啊啊啊啊啊啊啊小风车");
    }
    @PutMapping
    public R<SetmealVo> UpdateSetmeal(@RequestBody SetmealVo setmealVo){
        log.info("========="+setmealVo.toString());
        setmealService.updateWithDish(setmealVo);

        return R.success(setmealVo,"成功");
    }
//    @GetMapping("/list")
//    public R<List<Setmeal>> statusToStop(Setmeal setmeal){
//        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
//        log.info("===========================");
//        //排序
//        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
//        List<Setmeal> list = setmealService.list(queryWrapper);
//
//        return  R.success(list);
//    }
@GetMapping("/{id}")
public R<Setmeal> viewDish(@PathVariable Long id){

    SetmealVo byIdWithDish = setmealService.getByIdWithDish(id);

    return  R.success(byIdWithDish,"成功");
}
    @DeleteMapping
    //id数组名和前端参数一致
    public R<DishVo> deleteDish(@RequestParam List<Long>ids) {
        log.info("=============" + ids);
        setmealService.removeSetmealWithDish(ids);


        return R.success("套餐正在售卖中，不能删除");
    }
    @PostMapping("/status/{status}")
    public R<String> statusToStop(@PathVariable Integer status,@RequestParam List<Long>ids){

        log.info("=================="+status);
        setmealService.transStatus(ids,status);

        return  R.success("成功");
    }



}
